﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ventas.Data.Entities;

namespace Ventas.Data.Managers
{
    public class CategoryManager
    {
        //
        public static IList<Category> GetList(string filter)
        {
            using (VentasDbContext ctx = new VentasDbContext())
            {
                return ctx.Categorys.Where(u => u.Name.Contains(filter)).ToList();
            }
        }
        //
        public static Category Create(Category category)
        {
            using (VentasDbContext ctx = new VentasDbContext())
            {
                Category categoryDB = ctx.Categorys.Add(category);

                ctx.SaveChanges();
                return categoryDB;
            }
        }
        //
        public static Category Update(Category category)
        {
            using (VentasDbContext ctx = new VentasDbContext())
            {
                ctx.Entry(category).State = EntityState.Modified;
                ctx.SaveChanges();
                return category;
            }
        }
        //
        public static Category GetById(Guid id)
        {
            using (VentasDbContext ctx = new VentasDbContext())
            {
                Category categoryDB = ctx.Categorys.Find(id);
                if (categoryDB == null)
                {
                    throw new Exception("Category not found in data base");
                }
                return ctx.Categorys.Find(id);
            }
        }
        //
        public static void Delete(Guid id)
        {
            using (VentasDbContext ctx = new VentasDbContext())
            {
                Category categoryDB = ctx.Categorys.Find(id);
                if (categoryDB == null)
                {
                    throw new Exception("Category not found in data base");
                }
                ctx.Categorys.Remove(categoryDB);
                ctx.SaveChanges();
            }
        }
    }
}
